#include "music.h"
#include <QUuid>
#include <QMediaPlayer>
#include <QCoreApplication>

#include <QSqlQuery>
#include <QSqlError>

Music::Music()
    :_isLike(false),
    _isHistory(false)
{
}

Music::Music(const QUrl& url)
    :_isLike(false),
    _isHistory(false),
    _musicUrl(url)
{
    _musicID = QUuid::createUuid().toString();
    parseMediaMetaData();
}

void Music::setMusicID(const QString &musicID)
{
    _musicID = musicID;
}

void Music::setMusicName(const QString& musicName)
{
    _musicName = musicName;
}
void Music::setSignerName(const QString& signerName)
{
    _signerName = signerName;
}
void Music::setAlbumName(const QString& albumName)
{
    _albumName = albumName;
}
void Music::setDuration(const qint64 duration)
{
    _duration = duration;
}
void Music::setMusicUrl(const QUrl& musicUrl)
{
    _musicUrl = musicUrl;
}
void Music::setIsLike(bool isLike)
{
    _isLike = isLike;
}
void Music::setIsHistory(bool isHistory)
{
    _isHistory = isHistory;
}

QString Music::getMusicName()
{
    return _musicName;
}
QString Music::getSignerName()
{
    return _signerName;
}
QString Music::getAlbumName()
{
    return _albumName;
}
qint64 Music::getDuration()
{
    return _duration;
}
QUrl Music::getMusicUrl()
{
    return  _musicUrl;
}

QString Music::getLrcFilePath()
{
    QString musicPath = getMusicUrl().toLocalFile();
    musicPath.replace(".mp3" , ".lrc");
    musicPath.replace(".flac" , ".lrc");
    musicPath.replace(".mpga" , ".lrc");
    return musicPath;
}
bool Music::getIsLike()
{
    return _isLike;
}
bool Music::getIsHistory()
{
    return _isHistory;
}

QString Music::getMusicID()
{
    return _musicID;
}

void Music::parseMediaMetaData()
{
    //创建player
    QMediaPlayer player;

    //设置媒体源 依靠setMedia解析元数据
    player.setMedia(_musicUrl);

    //setMedia并不会等待 音乐加载完毕 而是立马返回
      //因此通过metaData获取元数据时，必须保证媒体已经加载完毕
      //因此需要通过循环 isMetaDataAvaliable方法检测数据是否有效
      //如果为true 加载完毕
    while(!player.isMetaDataAvailable())
    {
        QCoreApplication::processEvents();
    }

    //加载完毕 获取有效元数据
    if(player.isMetaDataAvailable())
    {
        _musicName = player.metaData("Title").toString().trimmed();
        _signerName = player.metaData("Author").toString().trimmed();
        _albumName = player.metaData("AlbumTitle").toString().trimmed();
        _duration = player.metaData("Duration").toLongLong();

        if(_musicName.isEmpty())
               _musicName = "歌曲未知";
        if(_signerName.isEmpty())
                _signerName = "歌手未知";
        if(_albumName.isEmpty())
                _albumName = "专辑未知";

        qDebug()<<"musicName: " << _musicName << " ;signerName: " << _signerName
                << " ;albumName: " << _albumName << " ;duration: " << _duration << endl;
    }
}

void Music::insertMusicToDB()
{
    QSqlQuery query;

    //查询music是否在SQL中
    query.prepare("select exists (select 1 from musicInfo where musicId = ?)");
    query.addBindValue(_musicID);

    if(!query.exec())
    {
        qDebug() << "查询失败" << query.lastError().text();
        return ;
    }

    if(query.next())
    {
        bool isExists = query.value(0).toBool();
        if(isExists)
        {
            //如果已经存在，更新
            query.prepare("update musicInfo set isLike = ? , isHistory = ? where musicid = ?");
            query.addBindValue(_isLike? 1 : 0);
            query.addBindValue(_isHistory? 1 : 0);
            query.addBindValue(_musicID);

            if(!query.exec())
            {
                qDebug() << "更新失败" << query.lastError().text();
                return ;
            }
           qDebug() << "更新成功";
        }
        else
        {
            query.prepare("insert into musicInfo(musicid , musicName , musicSigner , albumName , \
                                                musicUrl , duration , isLike , isHistory) \
                           values(?,?,?,?,?,?,?,?)");
            query.addBindValue(_musicID);
            query.addBindValue(_musicName);
            query.addBindValue(_signerName);
            query.addBindValue(_albumName);
            query.addBindValue(_musicUrl.toLocalFile());
            query.addBindValue(_duration);
            query.addBindValue(_isLike ? 1 : 0);
            query.addBindValue(_isHistory ? 1 : 0);

            if(!query.exec())
            {
                qDebug() << "插入失败" << query.lastError().text();
                return ;
            }
           qDebug() << "插入music信息 " << _musicName << " " << _musicID;
        }
    }
}
